package cn.car91.activity;

import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import cn.car91.R;
import cn.car91.adapter.ExposureCenterListAdapter;
import cn.car91.bean.Exposure;
import cn.car91.pager.ExposurePager;
import cn.car91.utils.Car91JsonUtil;
import cn.car91.view.HerilyAlertDialog;
import cn.car91.view.MyListView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.lidroid.xutils.view.annotation.event.OnRadioGroupCheckedChange;

public class UserExposureActivity extends BaseActivity {
    ExposurePager exposurePager = new ExposurePager();

    @ViewInject(R.id.exposure_list_scroll)
    ScrollView scrollView;
    @ViewInject(R.id.exposure_list)
    MyListView expsoureList;
    @ViewInject(R.id.none)
    View none;

    int state = 0;
    ExposureCenterListAdapter exposureListAdapter = null;

    private boolean addDate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_user_exposure);
        super.onCreate(savedInstanceState);

        exposureListAdapter = new ExposureCenterListAdapter(this, R.layout.view_center_exposure_item);
        expsoureList.setAdapter(exposureListAdapter);
        getData();

        scrollView.setOnTouchListener(onScrollTouchListener);

        ((TextView) findViewById(R.id.header_text)).setText("我的曝光");
    }

    @OnItemClick(R.id.exposure_list)
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
        final String eid = String.valueOf(exposureListAdapter.getExposureId(arg2));
        HerilyAlertDialog.Builder builder = new HerilyAlertDialog.Builder(UserExposureActivity.this);
        builder.setTitle("消息");
        if (state == 0) {
            builder.setMessage("您的投诉曝光等待审核中").create().show();
        } else if (state == 1) {
            builder.setMessage("根据您的投诉我们正在核实调查").create().show();
        } else if (state == 3) {
            builder.setMessage("对不起，该投诉被驳回了").setNegativeButton("取消", null)
                    .setPositiveButton("修改", new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Intent intent = new Intent();
                            intent.putExtra("eid", eid);
                            intent.putExtra("state", "3");
                            intent.setClass(UserExposureActivity.this, ExposurePushActivity.class);
                            startActivity(intent);
                        }
                    }).create().show();
        } else {
            Intent intent = new Intent();
            intent.putExtra("eid", eid);
            intent.putExtra("state", String.valueOf(state));
            intent.setClass(UserExposureActivity.this, ExposureDetailActivity.class);
            startActivity(intent);
        }
    }

    @OnRadioGroupCheckedChange(R.id.exposure_radio)
    public void onRadioCheckedChanged(RadioGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        switch (arg1) {
            case R.id.exposure_no:
                state = 0;
                break;
            case R.id.exposure_check:
                state = 1;
                break;
            case R.id.exposure_success:
                state = 2;
                break;
            case R.id.exposure_error:
                state = 3;
                break;
            default:
                break;
        }
        addDate = false;
        exposurePager.reset();
        getData();
    }

    private OnTouchListener onScrollTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            switch (arg1.getAction()) {
                case MotionEvent.ACTION_UP:
                    doOnBorderListener();
                    break;
            }
            return false;
        }

        private void doOnBorderListener() {

            if (expsoureList != null && expsoureList.getMeasuredHeight() <= scrollView.getScrollY() + scrollView.getHeight()) {
                //底部
                addDate = true;
                getData();
            } else if (scrollView.getScrollY() == 0) {
                //顶部
            }
        }
    };

    public void getData() {
        if (getCar91Application().getUser() != null) {
            exposurePager.getUserData(getCar91Application().getUser().getId(), String.valueOf(state), callbackList);
        }
    }

    /* 列表页面 */
    private void refreshListViewData(List data) {
        if (addDate) {
            exposureListAdapter.addData(data);
        } else {
            expsoureList.removeAllViewsInLayout();
            exposureListAdapter.setData(data);
        }
        exposureListAdapter.notifyDataSetChanged();
        addDate = true;
    }

    /**
     * 回调获取商家列表
     */
    private RequestCallBack callbackList = new RequestCallBack() {

        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            showTextWhenNone(false);
        }

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            // TODO Auto-generated method stub
            Toast.makeText(UserExposureActivity.this, "网络异常~", Toast.LENGTH_SHORT).show();

            showTextWhenNone(false);
        }

        @Override
        public void onSuccess(ResponseInfo arg0) {
            // TODO Auto-generated method stub
            List<Exposure> exposures = Car91JsonUtil.getDTOList(arg0.result.toString(), Exposure.class);

            if (exposures.size() > 0) {
                showTextWhenNone(false);
            } else if (!addDate) {
                showTextWhenNone(true);
            }
            refreshListViewData(exposures);
        }

        private void showTextWhenNone(Boolean show) {
            if (show) {
                if (scrollView.getVisibility() == View.VISIBLE) {
                    scrollView.setVisibility(View.GONE);
                }
                if (none.getVisibility() == View.GONE) {
                    none.setVisibility(View.VISIBLE);
                }
            } else {
                if (scrollView.getVisibility() == View.GONE) {
                    scrollView.setVisibility(View.VISIBLE);
                }
                if (none.getVisibility() == View.VISIBLE) {
                    none.setVisibility(View.GONE);
                }
            }
        }
    };
}
